package com.edu.study.base.questionKind.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.edu.study.base.stuQuestionCatalog.domain.StuQuestionCatalog;
import com.edu.study.base.stuQuestionCatalog.service.IStuQuestionCatalogService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.edu.study.base.questionKind.domain.StuQuestionKind;
import com.edu.study.base.questionKind.service.IStuQuestionKindService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题型分类Controller
 * 
 * @author jjt
 * @date 2021-05-17
 */
@RestController
@RequestMapping("/study/stuQuestionKind")
public class StuQuestionKindController extends BaseController
{
    @Autowired
    private IStuQuestionKindService stuQuestionKindService;

    @Autowired
    private IStuQuestionCatalogService stuQuestionCatalogService;
    /**
     * 查询题型分类列表
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:list')")
    @GetMapping("/list")
    public TableDataInfo list(StuQuestionKind stuQuestionKind)
    {
        startPage();
        List<StuQuestionKind> list = stuQuestionKindService.selectStuQuestionKindList(stuQuestionKind);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:list')")
    @GetMapping("/getList")
    public AjaxResult getList(StuQuestionKind stuQuestionKind)
    {
        List<StuQuestionKind> list = stuQuestionKindService.selectStuQuestionKindList(stuQuestionKind);
        return AjaxResult.success(list);
    }

    /**
     * 导出题型分类列表
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:export')")
    @Log(title = "题型分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StuQuestionKind stuQuestionKind)
    {
        List<StuQuestionKind> list = stuQuestionKindService.selectStuQuestionKindList(stuQuestionKind);
        ExcelUtil<StuQuestionKind> util = new ExcelUtil<StuQuestionKind>(StuQuestionKind.class);
        return util.exportExcel(list, "stuQuestionKind");
    }

    /**
     * 获取题型分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(stuQuestionKindService.selectStuQuestionKindById(id));
    }

    /**
     * 新增题型分类
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:add')")
    @Log(title = "题型分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StuQuestionKind stuQuestionKind)
    {
        return toAjax(stuQuestionKindService.insertStuQuestionKind(stuQuestionKind));
    }

    /**
     * 修改题型分类
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:edit')")
    @Log(title = "题型分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StuQuestionKind stuQuestionKind)
    {
        return toAjax(stuQuestionKindService.updateStuQuestionKind(stuQuestionKind));
    }

    /**
     * 删除题型分类
     */
    @PreAuthorize("@ss.hasPermi('study:stuQuestionKind:remove')")
    @Log(title = "题型分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stuQuestionKindService.deleteStuQuestionKindByIds(ids));
    }

    @Log(title = "标准题型")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(@RequestParam("catalogId") Long catalogId){
        List<StuQuestionCatalog> treeList = new ArrayList<>();
//        List<Long> kindIdList = new ArrayList<>();
        StuQuestionCatalog catalog = stuQuestionCatalogService.selectTreeDataById(catalogId);
//        List<Long> questionTypeIds = parseCatalog(catalog, kindIdList);
//        List<StuQuestionKind> kindList = stuQuestionKindService.selectStuQuestionKindListByIds(questionTypeIds);
//        Map map = new HashMap();
//        map.put("id",null);
//        map.put("label","题型");
//        if (kindList != null && kindList.size()>0) {
//            List<Map> childrenList = new ArrayList<>();
//            for (StuQuestionKind stuQuestionKind : kindList) {
//                Map child = new HashMap();
//                child.put("id",stuQuestionKind.getId());
//                child.put("label",stuQuestionKind.getQuestionTypeName());
//                childrenList.add(child);
//            }
//            map.put("children",childrenList);
//        }
        treeList.add(catalog);
        return AjaxResult.success(treeList);
    }
    public List<Long> parseCatalog(StuQuestionCatalog catalog ,List<Long> kindIdList){
        if(catalog != null && "0".equals(catalog.getIfCatalog()) && catalog.getQuestionTypeId() != null && catalog.getQuestionTypeId() > 0){
            kindIdList.add(catalog.getQuestionTypeId());
            return kindIdList;
        }
        if(catalog != null && catalog.getChildren() != null && catalog.getChildren().size() > 0){
            List<StuQuestionCatalog> children = catalog.getChildren();
            for (StuQuestionCatalog child : children) {
                parseCatalog(child,kindIdList);
            }
        }
        return kindIdList;
    }
}
